/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mr.qa.GlobalConfigs;

public class HttpUtil
{
    private static final Log log = LogFactory.getLog(HttpUtil.class);
    public static String getANewsLink()
    {
        String link = "";
        try
        {
            String urlStr = GlobalConfigs.NEWS_LIST_URL;
            if(urlStr == null || urlStr.trim().length() == 0)
            throw new Exception("No News Url is configured,please go to iask admin to set this value");
            URL  url = new URL(urlStr);
            HttpURLConnection httpCon  = (HttpURLConnection)url.openConnection();
            httpCon.connect();
            InputStream urlStream  =  httpCon.getInputStream();
            BufferedReader bufferReader = new BufferedReader
                    (new InputStreamReader(urlStream,"utf-8"));
            String htmlStr = "";
            String currentLine = "";
            while((currentLine = bufferReader.readLine()) != null)
            {
                htmlStr = htmlStr+currentLine;
            }
            Pattern p=null;
            Matcher m=null;
            p = Pattern.compile("<tr><td>");
            m = p.matcher(htmlStr);
            int start = 0;
            int end = 0;
            int i = 0;
            int max = 5;
            int[] starts = new int[max];
            while(m.find())
            {
                //start = m.start();
                end = m.end();
                starts[i] = end;
                //System.out.println(i+"="+end);                
                i = i+1;
                if(i >= max)break;
            }
            //System.out.println("step1 done");
            i = 0;
            p = Pattern.compile("</td></tr>");
            m = p.matcher(htmlStr);
            int[] ends = new int[max];
            while(m.find())
            {
                start = m.start();
                //end = m.end();
                ends[i] = start;
                i = i+1;
                if(i >= max)break;
            }
            //System.out.println("step2 done,"+starts.length);
            int rand = new Random().nextInt();
            //System.out.println("step3 done,"+rand);
            i = (rand >>> 1)%10;
            if(i >= 5)
                i = i-5;
            //System.out.println("step3 done,"+i);
            link = htmlStr.substring(starts[i], ends[i]);
            //System.out.println(i+",from:"+starts[i]+" to "+ends[i]+"="+link);
        }
        catch(Exception e)
        {
        	StackTraceElement[] element = e.getStackTrace();
        	for (int i = 0; i < element.length; i++) {
				StackTraceElement stackTraceElement = element[i];
				log.error(stackTraceElement);
			}
        }
        return link;
    }

}
